from ...models import *

# 获取单位词头的choices数据


def get_prefix_choices():
    unit_prefixes = UnitPrefix.objects.filter(valid_flag=True)
    prefix_choices = []
    for unit_prefix in unit_prefixes:
        prefix_choices.append({"value": str(unit_prefix.uuid), "label": "{}({})".format(
            unit_prefix.show_short_name(), unit_prefix.prefix_symbol)})
    return prefix_choices

# 获取基本量纲的choices数据


def get_basic_dimension_choices():
    basic_dimensions = BasicDimension.objects.filter(valid_flag=True)
    basic_dimension_choices = []
    for basic_dimension in basic_dimensions:
        basic_dimension_choices.append({"value": str(basic_dimension.uuid), "label": "{}({})".format(
            basic_dimension.show_short_name(), basic_dimension.basic_dimension_symbol)})
    return basic_dimension_choices

# 获取量纲的choices数据


def get_dimension_choices():
    dimensions = Dimension.objects.filter(valid_flag=True)
    dimension_choices = []
    for dimension in dimensions:
        dimension_choices.append(
            {"value": str(dimension.uuid), "label": "{}".format(dimension.show_short_name_with_dimension_consistency())})
    return dimension_choices

# 获取某一个量纲下的可用单位的choices数据


def get_valid_unit_choices_by_dimension(dimension_entity):
    units = Unit.objects.filter(dimension=dimension_entity, valid_flag=True)
    unit_choices = []
    for unit in units:
        unit_choices.append({"value": str(unit.uuid), "label": "{}({})".format(
            unit.show_short_name(), unit.unit_symbol)})
    return unit_choices

# 获取量类型的choices数据


def get_quantity_type_choices():
    from ...config.type_config import QuantityType
    quantity_types_choice = [
        {"value": tag.name, "label": tag.value} for tag in QuantityType]
    return quantity_types_choice

# 给定一个key，从一个map里返回对应的value


def get_value_from_map(key, map):
    if key in map:
        return map[key]
    return "无"

# 获取单位集的choices数据


def get_unit_set_choices():
    unit_sets = UnitSet.objects.filter(valid_flag=True)
    unit_set_choices = []
    for unit_set in unit_sets:
        unit_set_choices.append(
            {"value": str(unit_set.uuid), "label": "{}".format(unit_set.show_short_name())})
    return unit_set_choices

# 获取某个量可用且可见的单位集合的choices数据


def get_valid_show_unit_choices_by_quantity(quantity_entity):
    unit_choices = []
    quantity_unit_map_list = quantity_entity.quantity_unit_map_quantity.all()
    for quantity_unit_map in quantity_unit_map_list:
        if quantity_unit_map.unit.valid_flag and quantity_unit_map.unit.is_show:
            unit_choices.append({"value": str(quantity_unit_map.unit.uuid), "label": "{}({})".format(
                quantity_unit_map.unit.show_short_name(), quantity_unit_map.unit.unit_symbol)})
    return unit_choices

# 获取所有可用并且可见的单位的choices数据
def get_all_valid_show_unit_choices():
    units = Unit.objects.filter(valid_flag=True, is_show=True)
    unit_choices = []
    for unit in units:
        unit_choices.append({"value": str(unit.uuid), "label": "{}({})".format(
            unit.show_short_name(), unit.unit_symbol)})
    return unit_choices